﻿Imports System.Data.SqlClient
Imports System.Xml
Imports System.Windows.Forms.DataGrid

Public Class Frm_DonViTinh

    
    Dim clsdm As New Cls_DanhMuc

    Private Sub Frm_DonViTinh_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        clsdm.FillDataGirdView("select * from DonViTinh", Me.dtg, Me.dts)

    End Sub
    Function KiemTraTextBox() As Integer
        Dim str As String = ""
        If Me.txttendvt.Text.Trim = "" Then
            str += "Chưa có Tên ? & Chr(13)"
            txttendvt.Focus()
        End If
        If str <> "" Then
            MsgBox("Thông tin cân nhập:" & Chr(13) & Chr(13) & str, MsgBoxStyle.Information, "Thông Báo")
            Return 0
        Else
            Return 1
        End If
    End Function
#Region " Them xoa SUa"
    Private Sub btthoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btthoat.Click
        Me.Close()
    End Sub

    Private Sub btthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btthem.Click
        If Me.btthem.Text = "&Thêm" Then
            Me.btthem.Text = "&Hủy"
            Me.btluu.Enabled = True
            Me.btsua.Enabled = False
            Me.btxoa.Enabled = False
            Me.txttendvt.Text = ""
            Me.txtmadvt.Text = ""
            Me.txttendvt.Focus()
        Else
            Me.btthem.Text = "&Thêm"
            Me.txttendvt.Text = ""
            Me.btsua.Enabled = True
            Me.btxoa.Enabled = True
        End If
    End Sub
    Private Sub btluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btluu.Click
        If KiemTraTextBox() = 1 Then
            Dim cmd As New SqlCommand("ThemDVT", con)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.Add("@Tendvt", SqlDbType.NVarChar).Value = txttendvt.Text
            Try
                cmd.ExecuteNonQuery()
                Me.txttendvt.Text = ""
            Catch ex As Exception
                MsgBox("Lỗi Cập Nhật " & ex.Message, MsgBoxStyle.Critical, "Thông Báo")
            End Try
            Me.btthem.Text = "&Thêm"
            Me.btluu.Enabled = False
            Me.btsua.Enabled = True
            Me.btxoa.Enabled = True
        End If
        clsdm.FillDataGirdView("select * from DonViTinh", Me.dtg, Me.dts)
    End Sub
    Private Sub btxoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btxoa.Click
        If KiemTraTextBox() = 1 Then
            Dim strma, strten As String
            strma = dtg.CurrentRow.Cells(0).Value.ToString()
            strten = dtg.CurrentRow.Cells(1).Value.ToString()
            If MsgBox("Bạn có chắc xóa đơn vị : " & strten & "?", MsgBoxStyle.YesNo, "Thông Báo") = MsgBoxResult.Yes Then
                Try
                    Dim cmd As New SqlCommand("Xoadvt", con)
                    cmd.CommandType = CommandType.StoredProcedure
                    cmd.Parameters.Add("@Madvt", SqlDbType.Int).Value = txtmadvt.Text.Trim
                    cmd.ExecuteNonQuery()
                    Me.txttendvt.Text = ""
                Catch ex As Exception
                    MsgBox("Lỗi khi xóa" & ex.Message)
                    Exit Sub
                End Try
            End If
        End If
        clsdm.FillDataGirdView("select * from DonViTinh", Me.dtg, Me.dts)
    End Sub
    Private Sub btsua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btsua.Click
        If KiemTraTextBox() = 1 Then
            Try
                Dim cmd As New SqlCommand("SuaDVT", con)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@Madvt", SqlDbType.Int).Value = txtmadvt.Text.Trim
                cmd.Parameters.Add("@Tendvt", SqlDbType.NVarChar).Value = txttendvt.Text.Trim
                cmd.ExecuteNonQuery()
            Catch ex As Exception
                MsgBox("Lỗi khi Sửa" & ex.Message)
                Exit Sub
            End Try
        End If
        clsdm.FillDataGirdView("select * from DonViTinh", Me.dtg, Me.dts)
    End Sub
#End Region

    Private Sub dtg_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtg.SelectionChanged
        txtmadvt.Text = dtg.CurrentRow.Cells(0).Value.ToString()
        txttendvt.Text = dtg.CurrentRow.Cells(1).Value.ToString()
    End Sub
End Class